package com.itboye.mengwu.data.presenter;

import java.util.List;
import java.util.Observer;

import com.itboye.core.interfaces.ICompleteListener;
import com.itboye.core.object.BasePresenter;
import com.itboye.core.object.ResultEntity;
import com.itboye.mengwu.app.ui.main.MainPresenter;
import com.itboye.mengwu.data.interfaces.IAddressQuery;
import com.itboye.mengwu.data.model.AddressQueryModel;
import com.itboye.mengwu.data.repository.AddressResitory;

public class AddressQueryPresenter extends BasePresenter implements
		IAddressQuery<AddressQueryModel> {
	// 查询标识
	public static final String AddressQuerySuccessEvent = MainPresenter.class
			.getName() + "_AddressQuery_success";
	public static final String AddressQueryFailEvent = MainPresenter.class
			.getName() + "_AddressQuery_failure";

	// 删除标识
	public static final String AddressDeleteFailEvent = MainPresenter.class
			.getName() + "_AddressDelete_failure";
	public static final String AddressDeleteSuccessEvent = MainPresenter.class
			.getName() + "_AddressDelete_success";

	// 修改标识
	public static final String AddressUpdateFailEvent = MainPresenter.class
			.getName() + "_AddressUpdate_failure";
	public static final String AddressUpdateSuccessEvent = MainPresenter.class
			.getName() + "_AddressUpdate_success";

	// 添加标识
	public static final String AddressAddFailEvent = MainPresenter.class
			.getName() + "_AddressAdd_failure";
	public static final String AddressAddSuccessEvent = MainPresenter.class
			.getName() + "_AddressAdd_success";

	// 获取默认地址标识
	public static final String AddressDefaultFailEvent = MainPresenter.class
			.getName() + "_AddressDefault_failure";
	public static final String AddressDefaultSuccessEvent = MainPresenter.class
			.getName() + "_AddressDefault_success";

	// 设置默认地址标识
	public static final String AddressSetDefaultSuccessEvent = MainPresenter.class
			.getName() + "_AddressSetDefault_success";
	public static final String AddressSetDefaultFailEvent = MainPresenter.class
			.getName() + "_AddressSetDefualt_fail";

	// 获取单个收货地址
	public static final String AddressGetSingleSuccessEvent = MainPresenter.class
			.getName() + "_AddressGetSingle_success";
			public static final String AddressGetSingleFailEvent = MainPresenter.class
					.getName() + "_AddressGetSingle_fail";
	public AddressQueryPresenter(Observer observer) {
		super(observer);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void add(AddressQueryModel model) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
				IAddressQuery<AddressQueryModel> address = new AddressResitory(
						new ICompleteListener() {

							@Override
							public void success(ResultEntity result) {
								// TODO Auto-generated method stub
								result.setEventTag(Tag_Success);
								result.setEventType(AddressAddSuccessEvent);
								setChanged();
								notifyObservers(result);
							}

							@Override
							public void failure(ResultEntity result) {
								// TODO Auto-generated method stub
								result.setEventTag(Tag_Error);
								result.setEventType(AddressAddFailEvent);
								setChanged();
								notifyObservers(result);
							}
						});
				address.add(model);
	}

	@Override
	public void delete(AddressQueryModel model) {
		// TODO Auto-generated method stub
		IAddressQuery<AddressQueryModel> addressQuery = new AddressResitory(
				new ICompleteListener() {

					@Override
					public void success(ResultEntity result) {
						// TODO Auto-generated method stub
						result.setEventTag(Tag_Success);
						result.setEventType(AddressDeleteSuccessEvent);
						AddressQueryPresenter.this.setChanged();
						AddressQueryPresenter.this.notifyObservers(result);
					}

					@Override
					public void failure(ResultEntity result) {
						// TODO Auto-generated method stub
						result.setEventTag(Tag_Success);
						result.setEventType(AddressDeleteFailEvent);
						AddressQueryPresenter.this.setChanged();
						AddressQueryPresenter.this.notifyObservers(result);
					}
				});
		addressQuery.delete(model);
	}

	@Override
	public void update(AddressQueryModel model) {
		// TODO Auto-generated method stub
		IAddressQuery<AddressQueryModel> addressQuery = new AddressResitory(
				new ICompleteListener() {

					@Override
					public void success(ResultEntity result) {
						// TODO Auto-generated method stub
						result.setEventTag(Tag_Success);
						result.setEventType(AddressQuerySuccessEvent);
						AddressQueryPresenter.this.setChanged();
						AddressQueryPresenter.this.notifyObservers(result);
					}

					@Override
					public void failure(ResultEntity result) {
						// TODO Auto-generated method stub
						result.setEventTag(Tag_Success);
						result.setEventType(AddressQueryFailEvent);
						AddressQueryPresenter.this.setChanged();
						AddressQueryPresenter.this.notifyObservers(result);
					}
				});
		addressQuery.update(model);
	}

	@Override
	public void getInfo(AddressQueryModel model) {
		// TODO Auto-generated method stub

	}

	@Override
	public List<AddressQueryModel> query(AddressQueryModel model) {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 查询所有地址
	 */
	@Override
	public void queryAddress(String uid) {
		// TODO Auto-generated method stub
		IAddressQuery<AddressQueryModel> addressQuery = new AddressResitory(
				new ICompleteListener() {

					@Override
					public void success(ResultEntity result) {
						// TODO Auto-generated method stub
						result.setEventTag(Tag_Success);
						result.setEventType(AddressQuerySuccessEvent);
						AddressQueryPresenter.this.setChanged();
						AddressQueryPresenter.this.notifyObservers(result);
					}

					@Override
					public void failure(ResultEntity result) {
						// TODO Auto-generated method stub
						result.setEventTag(Tag_Success);
						result.setEventType(AddressQueryFailEvent);
						AddressQueryPresenter.this.setChanged();
						AddressQueryPresenter.this.notifyObservers(result);
					}
				});
		addressQuery.queryAddress(uid);
	}

	/**
	 * 获取单个地址
	 */
	@Override
	public void getDefaultAddr(String uid) {
		// TODO Auto-generated method stub
		IAddressQuery<AddressQueryModel> addressQuery = new AddressResitory(
				new ICompleteListener() {

					@Override
					public void success(ResultEntity result) {
						// TODO Auto-generated method stub
						result.setEventTag(Tag_Success);
						result.setEventType(AddressDefaultSuccessEvent);
						AddressQueryPresenter.this.setChanged();
						AddressQueryPresenter.this.notifyObservers(result);
					}

					@Override
					public void failure(ResultEntity result) {
						// TODO Auto-generated method stub
						result.setEventTag(Tag_Success);
						result.setEventType(AddressDefaultFailEvent);
						AddressQueryPresenter.this.setChanged();
						AddressQueryPresenter.this.notifyObservers(result);
					}
				});
		addressQuery.getDefaultAddr(uid);
	}
	
	/**
	 * 设置默认地址
	 */

	@Override
	public void setDefaultAddr(String uid, String id) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		IAddressQuery<AddressQueryModel> addressQuery = new AddressResitory(
				new ICompleteListener() {

					@Override
					public void success(ResultEntity result) {
						// TODO Auto-generated method stub
						result.setEventTag(Tag_Success);
						result.setEventType(AddressSetDefaultSuccessEvent);
						AddressQueryPresenter.this.setChanged();
						AddressQueryPresenter.this.notifyObservers(result);
					}

					@Override
					public void failure(ResultEntity result) {
						// TODO Auto-generated method stub
						result.setEventTag(Tag_Success);
						result.setEventType(AddressSetDefaultFailEvent);
						AddressQueryPresenter.this.setChanged();
						AddressQueryPresenter.this.notifyObservers(result);
					}
				});
		addressQuery.setDefaultAddr(uid, id);
	}

	/***
	 * 获取单个地址
	 */
	@Override
	public void getSingleAddr(String uid, String id) {
		// TODO Auto-generated method stub
		IAddressQuery<AddressQueryModel> addressQuery = new AddressResitory(
				new ICompleteListener() {

					@Override
					public void success(ResultEntity result) {
						// TODO Auto-generated method stub
						result.setEventTag(Tag_Success);
						result.setEventType(AddressGetSingleSuccessEvent);
						AddressQueryPresenter.this.setChanged();
						AddressQueryPresenter.this.notifyObservers(result);
					}

					@Override
					public void failure(ResultEntity result) {
						// TODO Auto-generated method stub
						result.setEventTag(Tag_Success);
						result.setEventType(AddressGetSingleFailEvent);
						AddressQueryPresenter.this.setChanged();
						AddressQueryPresenter.this.notifyObservers(result);
					}
				});
		addressQuery.getSingleAddr(uid, id);
	}

}
